<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\controller\AdminController;
/**
 * @ControllerAnnotation(title="店铺平台商品")
 */
class ShopPlatformGoods extends AdminController
{
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $page = input('page', 1, 'intval');
        $pageSize = input('page_size', 15, 'intval');

        $keyword = input('keyword', '', 'trim');

        $where = [];
        if (isset($this->user['shop_id'])) {
            $where[] = ['spg.shop_id', '=', $this->user['shop_id']];
        }

        if ($keyword) {
            $where[] = ['g.title', 'like', '%' . $keyword . '%'];
        }

        $list = \app\common\model\ShopPlatformGoods::where($where)->alias('spg')
            ->leftJoin('goods g', 'g.id=spg.goods_id');
        $totalCount = $list->count();

        $list = $list->order('spg.id desc')->page($page, $pageSize)
            ->field('spg.*,g.name as goods_title,g.show_price,g.img as goods_img')->select()->toArray();


        return success([
            'total_count' => $totalCount,
            'list' => $list
        ]);
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        $goodsId = array_unique(input('goods_id', []));

        if (!$goodsId) {
            return error('请选择商品');
        }

        $where = [];

        if (isset($this->user['shop_id'])) {
            $shopId = $this->user['shop_id'];
            $where[] = ['shop_id', '=', $shopId];
        } else {
            $shopId = input('shop_id', 0, 'intval');
            if (!$shopId) {
                return error('请指定店铺');
            }
            $where[] = ['shop_id', '=', $shopId];
        }
        $existGoodsId = \app\common\model\ShopPlatformGoods::where($where)->column('goods_id');

        $ids = array_diff($goodsId, $existGoodsId);

        if (!$ids) {
            return success([], '添加成功');
        }

        $insertData = [];
        foreach ($ids as $id) {
            $insertData[] = [
                'shop_id' => $shopId,
                'goods_id' => $id
            ];
        }

        $model = new \app\common\model\ShopPlatformGoods();
        $model->saveAll($insertData);

        return success([], '添加成功！');
    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function del()
    {

        $where = [];

        if (isset($this->user['shop_id'])) {
            $shopId = $this->user['shop_id'];
            $where[] = ['shop_id', '=', $shopId];
        } else {
            $shopId = input('shop_id', 0, 'intval');
            if (!$shopId) {
                return error('请指定店铺');
            }
            $where[] = ['shop_id', '=', $shopId];
        }

        $goodsId = array_unique(input('goods_id', []));

        if (!$goodsId) {
            return error('请选择商品');
        }

        \app\common\model\ShopPlatformGoods::where('shop_id', $shopId)->whereIn('goods_id', $goodsId)->useSoftDelete('delete_time', time())->delete();

        return success([], '删除成功');
    }
}
